//[ׂƂ] layer{^ROIPEN쓮F{^ł̓NXwA\Ȃ
//[ɂ璼ׂƂ] C[{^ɐFtAC[荞ݎ̐FÃXy[X͎gĂȂ̂ɉ͂Ă
//156x156pixelȂ7frame\
//}jAFEnterInputBoxI

//̓p[^(ftHglςꍇ͂ύX)
FPS=30
̓t[=100	//Total Frame Num<Change here>
撷=300
臒l=100	//Color threshold<Change here>
͖x=4	//Pixel skip<Change here>
XLbvt[=4	//Frame skip<Change here>
͑x=0	//WaittimePerFrame<Change here>
M=10	//M̕
Œᓖ=1	//̐ȉpixelł̓}EX݂Ȃ Minimum pixel num<Change here>
͏dR=0.0	//dɌqR̒2
Bin=10
Bint[=0

SplitCorrectMode=0	//1:miniscope mode
FixX=344	//Width of priginal right part
FixWidth=135	//Width of priginal left part

//CVX=1280	//1024 (+256)
//CVY=960		//768 (+192)
CVX=640	//1024 (+256)
CVY=480		//768 (+192)
#include "hspcv.as"		//摜ȆpCu
#include "hspext.as"	//tH_Ip̃Cu
#include "user32.as"	//gOXCb`pCu
#include "hgimg3.as"	//p
#define ő`l 99
#define 捶X 5
#define 捶Y 45
#define {^ő吔 40
#define ob`Iʕ 1200
#define ob`Iʍ 1000

ʍX=240	//Cʂ̍̍W
ʍY=10
ʕ=1200 //1200 Cʂ̃TCY
ʍ=700
pX=200	//p[^̃TCY
pY=800

Oʕ=0
Oʍ=0
{^ő吔vX1={^ő吔+1

#define debag 0 //œǂݍ܂ꍇ1
screen 0,ʕ,ʍ,0,ʍX,ʍY			//\
title "Mr. Behavior - Tracker"
buffer 2,ʕ,ʍ					//CVRs[p
buffer 3,ʕ,ʍ					//ʃNAp
buffer 4,1,1	//OՉ摜pobt@
//buffer5-15̓C[
buffer 96,ʕ,ʍ+1	//ROIZ[up
buffer 97,ʕ,ʍ	//ROIꎞp
screen 99,ʕ,ʍ,0,1225,10	//Ұ\p
screen 98,pX,pY,0,5,5	//͏͗p

title "Params"
gsel 98,2

color 255,255,255
boxf 0,0,ʕ,ʍ
gsel 0
gmode 4,,,100
dim ButtonLight,30
//{^NX
dim {gp,{^ő吔
dim {,{^ő吔	//0:Open,AddChannel,Export 1:AZAPen, 2:Ch layer, 3:Ch selecter, 4:Analyze, 5:Export
dim {X,{^ő吔
dim {Y,{^ő吔
dim {,{^ő吔
dim {,{^ő吔
dim {,{^ő吔	//1or-1
dim {C,{^ő吔	//Ή郌C[
sdim {,,{^ő吔
dim {F,{^ő吔vX1,3
//

dim ͂邩,ʕ,ʍ	//l1̏ꍇ͐FhĂ邽߉͂
ddim ؍ݎ,12
dim ړ,12
dim N,12	//areaɓ =EntryNumber
dim ڐG,12	//dSł͂ȂApixelGAɓ
ddim ڐG,12	//dSł͂ȂApixelGAɓ
ddim ؍ݎBin,12,1000
dim ړBin,12,1000
dim NBin,12,1000
dim ڐGBin,12,1000
ddim ڐGBin,12,1000
ddim t[ړ,10000	//1framëړ
dim ؍݈ʒuX,10000	//}EX̑؍ݍW
dim ؍݈ʒuY,10000

dim Ot[,12	//Õt[ɃC[ɑ؍݂Ă
ddim l,4	//human count
dim l,4	//human count
ddim lBin,4,1000	//human count
dim lBin,4,1000	//human count

dim lgK[,4	//human count
=0
Num=0	//ėp
SeqPath=""
PIt@C=""

dim X,13	//̈ʒu
dim Y,13
repeat 13
	Y(cnt)=10000
loop
sdim ,13,100	//̖{
(0)="Video frame number per second"
(1)="Length of the video."
(2)="Brightness thereshold to detect the subject from background."
(3)="Minimum number of subject detecting sampling points to move the tracking cursor. "
(4)="Specify how much to downsample the video resolution (e.g. If 4, the analysis resolution is reduced to 1/4 \nboth vertically and horizontally. If 1, the downsample doesn't take place). Larger downsamples allow for less processing time."
(5)="Specify how often to thin out the video frames (e.g. If 5, only one frame will be analyzed every five frames. If 1, all frames will be analyzed)."
(6)="Duration of bin."
(7)="Wait time per frame during analysis. Set 0 if you want the fastest analysis."
(8)="Looseness length between center of mass of the subject and the track cursor. \n(Set 0 except when the mouse shows abnormal gait which confounds true moved distance)"
(9)="Size of ROI Pen and Eraser."
(10)="Check if the subject is white."
(11)="If checked, the track cursor stays at the position where the animal is last deteced.\nUncheck this for the Light/Dark chamber test."
(12)="Check if the subject in on metal grid floor."
I=-1	//݃[U[IĂԍ
ԍ=0	//݂̐ԍ
=200	//̕
=50	//̍

repeat 30
	ButtonLight(cnt)=-1
loop

=1000000
sdim ,
sdim ,
sdim DistancePerFrame,
sdim t[,
sdim MouseCoordinate,
//notesel DistancePerFrame	//ꂼ̃t[̈ړ(pixel)
//notesel 

O=""
pX=""
ږ1=""
ږ2=""
ږ3=""
ږ4=""
ږ5=""
ږ6=""
ږ7=""
1=""
2=""
3=""
4=""
5=""
6=""
7=""
dim ڐGԃgK[,12	//ڐGԂ̏Ŏgpit[j
dim ڐG񐔃gK[,12	//ڐG񐔂̏ŎgpiڐGSȂt[ł̂ݏj
C{^X=5
C\{^Y=580
CI{^Y=650
쐻C{^=0	//=Ă郌C
IC=1
ICF=0
ICF=0
ICF=0
샂[h=-1
`l=0
obt@=5	//C[obt@[5`
`l=0
͒=0
Cnt=0
ړ=0
X=0	//臒lzsNZXW
Y=0	//臒lzsNZYW
=0	//臒lzsNZ
݂Bin=0

}EXX=0.0
}EXY=0.0
}EXPX=0.0
}EXPY=0.0
|CgPx=0

n_X = 0
n_Y = 0
I_X = 0
I_Y = 0
RoiPhase = 0	//0I@1n_I@2I_I
RoiX1 = 0
RoiY1 = 0
RoiX2 = 0
RoiY2 = 0
{^Vtg = 0
dX=0.0		//d̈ʒu
dY=0.0
dPX=0.0		//1frameȌd̈ʒu
dPY=0.0

dR=0.0	//dɌqR̒
d}EX=0.0	//}EXƏd̋^2
dRs=0.0
d}EXpx = 0.0
d}EXpx\ = 0.0
ŏ̃}EX=1	//ŏږL
Message=0	//1ʏ͏I 2ob`͏I
}EXXg=0
}EXo=0
ob`͏I=0	//1̏ꍇanalysisƍĂуob`͂ɂȂ
IsWhiteAnimal=0
LeaveCursorWhileLost=1
BackNoiseFilterOn=1
斢J=1	//1̏ꍇ܂1JĂȂƂӖ
͊Jn~=0
so=0
dim LayerExist,11	//1̏ꍇ͂̓_̃C[

exist "VideoFPS.at"
if strsize!=-1{
	bload "VideoFPS.at",FPS	//Load
}
exist "VideoLength.at"
if strsize!=-1{
	bload "VideoLength.at",撷	//Load
}
exist "ColorThreshold.at"
if strsize!=-1{
	bload "ColorThreshold.at",臒l	//Load
}
exist "MinimumPixelNum.at"
if strsize!=-1{
	bload "MinimumPixelNum.at",Œᓖ	//Load
}
exist "DownSample.at"
if strsize!=-1{
	bload "DownSample.at",͖x	//Load
}
exist "FrameSkip.at"
if strsize!=-1{
	bload "FrameSkip.at",XLbvt[	//Load
}
exist "BinDur.at"
if strsize!=-1{
	bload "BinDur.at",Bin	//Load
}
exist "WaitTimePerFrame.at"
if strsize!=-1{
	bload "WaitTimePerFrame.at",͑x	//Load
}
exist "Looseness.at"
if strsize!=-1{
	bload "Looseness.at",͏dR	//Load
}
exist "PenSize.at"
if strsize!=-1{
	bload "PenSize.at",M	//Load
}
exist "IsWhiteAnimal.at"
if strsize!=-1{
	bload "IsWhiteAnimal.at",IsWhiteAnimal	//Load
}
exist "LeaveCursorWhileLost.at"
if strsize!=-1{
	bload "LeaveCursorWhileLost.at",LeaveCursorWhileLost	//Load
}
exist "BackNoiseFilterOn.at"
if strsize!=-1{
	bload "BackNoiseFilterOn.at",BackNoiseFilterOn	//Load
}
ProgramPath=dir_cur	//svÕpXێ
a=0
X=5:Y=5:=89:=35
{gp(a)=1:{(a)=0:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Open(.avi)"	//0
X+=+2++2
a++
{gp(a)=1:{(a)=0:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Add Area"	//1
X+=+2
a++
{gp(a)=1:{(a)=1:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Rect"			//2
X+=+2
a++
{gp(a)=1:{(a)=1:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Circle"			//3
X+=+2
a++
{gp(a)=1:{(a)=1:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Pen"		//4
X+=+2
a++
{gp(a)=1:{(a)=1:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="RectDel"			//5
X+=+2
a++
{gp(a)=1:{(a)=1:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="CircleDel"			//6
X+=+2
a++
{gp(a)=1:{(a)=1:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Eraser"		//7
X+=+2
a++
{gp(a)=1:{(a)=0:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="SaveArea"	//8
X+=+2
a++
{gp(a)=1:{(a)=0:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="LoadArea"	//9
X+=+2
a++
{gp(a)=1:{(a)=4:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Analyze"	//10
X+=+2
a++
{gp(a)=1:{(a)=0:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="Export"		//11
X=5:X+=+2
a++
{gp(a)=1:{(a)=0:{X(a)=X:{Y(a)=Y:{(a)=:{(a)=:{(a)=-1:{(a)="OpenMulti"	//12

//{^ݒu
gsel 98
̓TCYX=0
̓TCYY=0
TvOX=0
TvOY=0
{^Y=30
VtgY=0
UiX=10
pos UiX,{^Y:	input FPS:	{^Y+=55
pos UiX,{^Y:	input 撷:	{^Y+=55
pos UiX,{^Y:	input 臒l:	{^Y+=55
pos UiX,{^Y:	input Œᓖ:	{^Y+=55
pos UiX,{^Y:	input ͖x:	{^Y+=55
pos UiX,{^Y:	input XLbvt[:	{^Y+=55
pos UiX,{^Y:	input Bin:	{^Y+=55
pos UiX,{^Y:	input ͑x:	{^Y+=55
pos UiX,{^Y:	input ͏dR:	{^Y+=55
pos UiX,{^Y:	input M:	{^Y+=55
objsize 170, 20
pos UiX,{^Y:	chkbox "White animal",IsWhiteAnimal:	{^Y+=55
pos UiX,{^Y:	chkbox "Leave cursor when lose track",LeaveCursorWhileLost:	{^Y+=55
pos UiX,{^Y:	chkbox "Background noise filter",BackNoiseFilterOn:	{^Y+=55

gsel 0
//objsize 80,20
//pos 1000,350:	chkbox "[h",chbox
}EXo=1
ŏ=1
gosub *AddChannel	//ŏɃC[P̂ݒǉ
ŏ=0
ŏ̃t[=1
dim RoiSaveArray,ʕ,ʍ+1
onexit *exit

if Debug=1{	//debug[h̎͌܂pX̓NɊJ
	gsel 2
	cls
	cvbuffer 0,1024,768	//CVobt@(p1Fobt@[ԍAp2,3Fobt@[TCY)
	cvopenavi "G:\\SI\\140222 acb1 SI2\\14N02221713-O(1FRX3@)-ԑg擾.avi",0	//avit@Cobt@[(p2)ɊJiŕKvj
	cvgetavi			//̃R}cvobt@[ɃRs[
	cvflip 0,0	//摜㉺t](Ȃ㉺܂ɂȂ)
	cvgetimg 0	//buffer2։摜]
	gsel 0
}


REPEAT	//Main Roop
	//**********************************************************************************************************************************
	mx=ginfo(0)-ginfo(4)-2
	my=ginfo(1)-ginfo(5)-26
	stick St
	stick StNoTrg,256
	getkey gkZ,90
	getkey gkX,88
	getkey gkC,67
	getkey gkV,86
	if St = 32{
		gsel 99,1
		gsel 0,1
	}
	gsel 98
	mxPara=ginfo(0)-ginfo(4)-2
	myPara=ginfo(1)-ginfo(5)-26
	gsel 0
	if ͒=0{
		if st=256{
			repeat {^ő吔
				if {gp(cnt)=0:break
				if mx>={X(cnt) & my>={Y(cnt) & mx<={X(cnt)+{(cnt) & my<={Y(cnt)+{(cnt){
					if {(cnt)=1{		//AZAPen
						if {(cnt)=-1{	//{^ȊO񉟂Ԃɂ
							repeat {^ő吔
								if {(cnt)=1:{(cnt)=-1
							loop
							{(cnt)=1
							샂[h=cnt
						}
					}
					if {(cnt)=2{		//Ch layer
						{(cnt)*=-1
					}
					if {(cnt)=3{		//Ch selecter
						if {(cnt)=-1{	//{^ȊO񉟂Ԃɂ
							repeat {^ő吔
								if {(cnt)=3:{(cnt)=-1
							loop
							{(cnt)=1
							IC={C(cnt)
						}
					}
					if cnt=8{	//C[ۑ
						gosub *SaveROI
					}
					if cnt=9{	//C[J
						gosub *LoadROI
					}
					if cnt=11{	//GNX|[g
						dialog "",17,
						if stat=1{	
							SaveName=refstr
							SaveName2=""
							split SaveName, ".", SaveName2
							
							notesel 
							notesave SaveName2+"_Result.csv"
							notesel DistancePerFrame
							notesave SaveName2+"_DistancePerFrame.csv"
							notesel t[
							notesave SaveName2+"_RestingPerArea.csv"
							notesel MouseCoordinate
							notesave SaveName2+"_CoordinatePerFrame.csv"
						}
					}
					if cnt=12{	//OpenMultiꂽ
						selfolder  SeqPath,"" //
						if stat=0{
							screen 6,ob`Iʕ,ob`Iʍ,0,ʍX,ʍY
							title "Click movie files for the sequence analysis"
							sdim filename,1000,99	//t@Cێp
							sdim filename2,1000,99	//t@Cێpi͏ԁj
							sdim list,16384			//1024
							dirlist list, SeqPath+"\\*.*",1	//tH_̃t@C擾list֑
							file_num=stat				//t@C
							//dialog str(file_num),0,""
							repeat file_num
								filename2(cnt)=""
							loop
							index=0						//oŏ̈ʒu
							repeat file_num
								getstr filename(cnt),list,index,,	//listsŋ؂ꂽ𔲏o
								index+=strsize			//oindexi߂
							loop
							color 0,0,0
							LUX=30		//t@Cꗗ(filename)c̍W
							LUY=30
							LUX2=620	//t@Cꗗ(filename2)c̍W
							LUY2=30
							repeat file_num	//t@Cꗗ`
								pos LUX,20*cnt+LUY
								mes cnt+1
								pos LUX+20,20*cnt+LUY
								mes filename(cnt)
							
								pos LUX2,20*cnt+LUY2
								mes cnt+1
								pos LUX2+20,20*cnt+LUY2
								mes filename2(cnt)
							loop
							now_num=0	//݂̏ԁifilename2ł̗vfƂĎgpj
							repeat	//t@CIC[v
								repeat file_num
									if filename(cnt)!=""{
										mx=ginfo(0)-ginfo(4)-3				//}EẌʒu擾
										my=ginfo(1)-ginfo(5)-24
										stick st,256
										if st=256{
											if mx>LUX & mx<LUX+300 & my>20*cnt+LUY & my<20*cnt+LUY+20{
												filename2(now_num)=filename(cnt)	//Ofilename2Ɏ󂯓n
												now_num++
												filename(cnt)=""
												color 255,255,255
												boxf 0,0,ob`Iʕ,ob`Iʍ
												color 0,0,0
												repeat file_num						//t@Cꗗĕ`
													pos LUX,20*cnt+LUY
													mes cnt+1
													pos LUX+20,20*cnt+LUY
													mes filename(cnt)
							
													pos LUX2,20*cnt+LUY2
													mes cnt+1
													pos LUX2+20,20*cnt+LUY2
													mes filename2(cnt)
												loop
											}
										}
									}
								loop
								
								color 100,100,200
								boxf 20,5,120,25
								pos 60,7
								color 0,0,0
								mes "OK"
								
								color 100,100,200
								boxf 125,5,225,25
								pos 153,7
								color 0,0,0
								mes "Cancel"
								if (st=256 & mx >=20 & my>=5 & mx<=120 & my<=25) | file_num=now_num{	//OKꂽ
									A̓[h=1
									A͐=0
									sdim filename,50,99	//t@Cێp
									//screen 6,1200,1000,2,5,5
									screen 6,1200,1000,2,ʍX,ʍY
									
									O=filename2(0)
									pX=SeqPath+"\\"+filename2(0)
									gsel 2
									cls
									cvcloseavi
									cvbuffer 0	//CVobt@(p1Fobt@[ԍAp2,3Fobt@[TCY)
									cvopenavi pX,0	//avit@Cobt@[(p2)ɊJiŕKvj
									cvgetavi			//̃R}cvobt@[ɃRs[
									gosub *MainWindowResize
									cvflip 0,0	//摜㉺t](Ȃ㉺܂ɂȂ)
									cvgetimg 0	//buffer2։摜]
									斢J=0
									gsel 0
									break
								}
								if st=256 & mx >=125 & my>=5 & mx<=225 & my<=25{	//Cancelꂽ
									sdim filename,99	//t@Cێp
									sdim filename2,99	//
									screen 6,1200,1000,2,5,5
									break
								}
								redraw 1
								redraw 0
								await 16
							loop
						}
					}
					if {(cnt)=4 & cnt=10{		//Analyzeꂽ(͕ϐɏ)
						if 斢J=0{
							gosub *StartAnalysis
						}
						if 斢J=1{
							dialog "Open movie first",1,""
						}
					}
					if cnt=0:gosub *Open
					if cnt=1:gosub *AddChannel
					gsel 99,1
					gsel 0,1
				}
			loop
		}
		if StNoTrg=256 & my>45 & mx<ʕ & my<C\{^Y-25{
			
			
			if 샂[h=2 | 샂[h=3 | 샂[h=5 | 샂[h=6{	//͈͎wi~orlpj taggg
				gsel IC+4
				if RoiPhase=0{	//n_NbN
						n_X = mx
						n_Y = my
						RoiPhase=1
				}
				if RoiPhase=1{	//hbO
						I_X = mx
						I_Y = my
				}
				gsel 0
			}
			
			if 샂[h=4{	//Mh
				if(mx < ʕ){
					gsel IC+4
					gosub *GetSelectedLayerColor
					repeat M
						ocnt=cnt
						repeat M
							pset mx+cnt-(M/2),my+ocnt-(M/2)
						loop
					loop
					//pset mx,my//:pset mx-1,my
					if ̓TCYX < mx+M-1-M/2: ̓TCYX = mx+M-1-M/2
					if ̓TCYY < my+M-1-M/2: ̓TCYY = my+M-1-M/2
					gsel 0
				}
			}
			if 샂[h=7{	//S
				gsel IC+4
				color 0,0,0
				repeat M
					ocnt=cnt
					repeat M
						pset mx+cnt-(M/2),my+ocnt-(M/2)
					loop
				loop
				pset mx,my:pset mx-1,my
				gsel 0
			}
		}
		if StNoTrg!=256 &RoiPhase = 1{
			if 샂[h=2 | 샂[h=5{
				gsel IC+4
				if 샂[h=2:gosub *GetSelectedLayerColor	//IC[̐F擾
				if 샂[h=5:color 0,0,0
				boxf RoiX2,RoiY2,RoiX1,RoiY1	//lp``
				if 샂[h=2{
					if ̓TCYX < RoiX1: ̓TCYX = RoiX1
					if ̓TCYX < RoiX2: ̓TCYX = RoiX2
					if ̓TCYY < RoiY1: ̓TCYY = RoiY1
					if ̓TCYY < RoiY2: ̓TCYY = RoiY2
				}
				RoiPhase = 0	//hbOIROI
			}
			if 샂[h=3 | 샂[h=6{
				gsel IC+4
				if 샂[h=3:gosub *GetSelectedLayerColor	//IC[̐F擾
				if 샂[h=6:color 0,0,0
				circle RoiX2,RoiY2,RoiX1,RoiY1	//~``
				if 샂[h=3{
					if ̓TCYX < RoiX1: ̓TCYX = RoiX1
					if ̓TCYX < RoiX2: ̓TCYX = RoiX2
					if ̓TCYY < RoiY1: ̓TCYY = RoiY1
					if ̓TCYY < RoiY2: ̓TCYY = RoiY2
				}
				RoiPhase = 0	//hbOIROI
			}
		}
	}
	//**********************************************************************************************************************************
	gsel 0
	gmode 0
	pos 0,0:color 0,0,0:gcopy 3,0,0,ʕ,ʍ	//ʃNA
	if ͒=1{
		gsel 0
		
		Cnt++
		݂Bin=(Cnt*XLbvt[)/(Bin*FPS)+1
		if Cnt=0{	//͊JnɌĂ΂鏉
			//dX=}EXX	//ŏɏd̈ʒu}EẌʒuɈړ
			//dY=}EXY
			dR=͏dR*͏dR
		}
		cvgetavi			//CVobt@ɓڰщ摜ۑ
		Repeat XLbvt[-1	//XLbv
			cvgetavi			//CVobt@ɓڰщ摜ۑ
		Loop
		gsel 2
		cvflip 0,0
		cvgetimg 0			//CVobt@[buffer2։摜]
		ŏ̃t[=0	//gK[
		gsel 0
		gmode 0
		
		if SplitCorrectMode=0{
			pos 捶X,捶Y
			gcopy 2,0,0,ʕ,ʍ	//Cʂ։摜]
		}
		if SplitCorrectMode=1{
			pos 捶X,捶Y
			gcopy 2,FixX,0,FixWidth,ʍ	//Cʂ։摜] 
			pos 捶X+FixWidth,捶Y
			gcopy 2,0,0,FixX,ʍ	//Cʂ։摜] 
		}
		//摜ROOP**************************************************************************
		=0
		X=0
		Y=0
		TvOX=̓TCYX/͖x
		TvOY=̓TCYY/͖x
		
		Repeat TvOY	
			ocnt=cnt
			Repeat TvOX
				kcnt=cnt
				if ͂邩(kcnt*͖x,ocnt*͖x)=1{
					pget kcnt*͖x,ocnt*͖x
					R=ginfo(16):G=ginfo(17):B=ginfo(18)
					Px=R+G+B	//PxZo
					if (IsWhiteAnimal = 0 & Px<=臒l) | (IsWhiteAnimal = 1 & Px>=臒l){	//臒lzꍇ
						if BackNoiseFilterOn = 1{	//wimCYtB^[ȈꍇA8}Xɓ肪邩肵A2}Xȉ炱̃}X̓S~oĂƌȂēȂ
							͓=0
							Trg=0
							Repeat 8
								if cnt=0{
									if ocnt>0{
										pget kcnt*͖x,(ocnt-1)*͖x
									}
								}
								if cnt=1{
									if kcnt<TvOX & ocnt>0{
										pget (kcnt+1)*͖x,(ocnt-1)*͖x
									}
								}
								if cnt=2{
									if kcnt<TvOX{
										pget (kcnt+1)*͖x,ocnt*͖x
									}
								}
								if cnt=3{
									if kcnt<TvOX & ocnt<TvOY{
										pget (kcnt+1)*͖x,(ocnt+1)*͖x
									}
								}
								if cnt=4{
									if ocnt<TvOY{
										pget kcnt*͖x,(ocnt+1)*͖x
									}
								}
								if cnt=5{
									if kcnt>0 & ocnt<TvOY{
										pget (kcnt-1)*͖x,(ocnt+1)*͖x
									}
								}
								if cnt=6{
									if kcnt>0{
										pget (kcnt-1)*͖x,ocnt*͖x
									}
								}
								if cnt=7{
									if kcnt>0 & ocnt>0{
										pget (kcnt-1)*͖x,(ocnt-1)*͖x
									}
								}
								R=ginfo(16):G=ginfo(17):B=ginfo(18)
								Px=R+G+B	//PxZo
								if (IsWhiteAnimal = 0 & Px<=臒l) | (IsWhiteAnimal = 1 & Px>=臒l){	//臒lzꍇ
									͓++
									if ͓>2{	//͂3_ȏ㓖sNZ
										Trg=1
										break
									}
								}
							Loop
						}
						if BackNoiseFilterOn = 0{	//wimCYtB^[It̏ꍇ
							Trg=1
						}
						if Trg=1{	//̃}XƌȂꂽꍇ
							color 255,255,255
							pset kcnt*͖x,ocnt*͖x
							++
							X+=kcnt*͖x
							Y+=ocnt*͖x
							Repeat 쐻C{^	//ڐGԁA񐔂ŏ
								gsel 5+cnt
								pget kcnt*͖x,ocnt*͖x
								R=ginfo(16):G=ginfo(17):B=ginfo(18)
								if R!=0 | G!=0 | B!=0{
									if ڐGԃgK[(1+cnt) = 0{
										ڐG(1+cnt)+=1.0*XLbvt[/FPS
										ڐGBin(1+cnt, ݂Bin)+=1.0*XLbvt[/FPS
										ڐGԃgK[(1+cnt) = 1
									}
									if ڐG񐔃gK[(1+cnt) = 0{
										ڐG(1+cnt)++
										ڐGBin(1+cnt, ݂Bin)++
										ڐG񐔃gK[(1+cnt) = 1
									}
								}
							Loop
							gsel 0
						}
					}
				}
			Loop
		Loop
		Repeat 쐻C{^
			if ڐGԃgK[(1+cnt) = 0{	//SpixelȂڐGȂȂƌȂ
				ڐG񐔃gK[(1+cnt) = 0
			}
			ڐGԃgK[(1+cnt) = 0	//gK[
		Loop
		//}EẌʒủ
		if >0{
			}EXX=double(X/)
			}EXY=double(Y/)
			if Cnt=0{	//͊JnɌĂ΂鏉
				dX=}EXX	//ŏɏd̈ʒu}EẌʒuɈړ
				dY=}EXY
				}EXPX=}EXX
				}EXPY=}EXY
			}
		}
		//d̈ʒuXV
		if ͏dR{	//R͂Ă
				dPX=dX
				dPY=dY 
				d}EX=(dX-}EXX)*(dX-}EXX)+(dY-}EXY)*(dY-}EXY)
				if dR < d}EX{	//Rdƃ}EX̋
					dRs = sqrt(d}EX) - sqrt(dR)
					if dX != }EXX || dY != }EXY{
						//px擾
						d}EXpx = atan(}EXY-dY, }EXX-dX)	//y,x
						d}EXpx\ = atan(}EXY-dY, }EXX-dX)*180.0/M_PI	//y,x
					}
					dX += cos(d}EXpx)*dRs	//dړ
					dY += sin(d}EXpx)*dRs
					
				}
		}
		//}EXyѐ̃}[N\
		color 255,0,0
		if >Œᓖ | LeaveCursorWhileLost=1{
			if ͏dR != 0{
				color 50,255, 50
				line dX, dY, }EXX, }EXY
				//color 0, 0, 255
				circle dX-4.0,dY-4.0, dX+4.0,dY+4.0
				//line dX-4,dY,dX+4,dY:	line dX,dY+4,dX,dY-4	//d̈ʒu\ŕ\
				//color 255, 0, 0
			}
			color 255,100,100
			line }EXX-6.0,}EXY,}EXX+6.0,}EXY:	line }EXX,}EXY+6.0,}EXX,}EXY-6.0	//}EẌʒu\ŕ\
		}
		//}EXyяd̍W̋L^
		if ͏dR = 0{
			؍݈ʒuX(Cnt)=}EXX
			؍݈ʒuY(Cnt)=}EXY
		}
		if ͏dR != 0{
			؍݈ʒuX(Cnt)=dX
			؍݈ʒuY(Cnt)=dY
		}
		//}EXo=0
		Repeat 쐻C{^	//C[ł̈ړA؍ݎԓXV
			gsel 5+cnt
			pget }EXX,}EXY
			R=ginfo(16):G=ginfo(17):B=ginfo(18)
			if R!=0 | G!=0 | B!=0{	//}EX̃J[\̃C[ƏdȂĂ
				if >=Œᓖ | so=1 & LeaveCursorWhileLost=1{	//}EX̃J[\\ꍇ
					so=1
					؍ݎ(1+cnt)+=1.0*XLbvt[/FPS	//؍ݎԑ
					؍ݎBin(1+cnt, ݂Bin)+=1.0*XLbvt[/FPS
					C؍݃t[(1+cnt, Cnt)=1
					
					if ͏dR = 0{
						fsqr ړ,((}EXPX-}EXX)*(}EXPX-}EXX)+(}EXPY-}EXY)*(}EXPY-}EXY))
					}
					if ͏dR != 0{
						fsqr ړ,((dPX-dX)*(dPX-dX)+(dPY-dY)*(dPY-dY))
					}
					if Ot[}EXo=1{
						ړ=0.0
					}
					ړ(1+cnt)+=ړ	//ړ
					ړBin(1+cnt,݂Bin)+=ړ	//ړ
					t[ړ(Cnt)=ړ
					
					if Ot[(1+cnt)=0{
						N(1+cnt)++
						NBin(1+cnt, ݂Bin)++
						Ot[(1+cnt)=1
					}
					//}EXo=1
				}
			}
			if R=0 & G=0 & B=0{
				Ot[(1+cnt)=0
			}
		Loop
	
		if Cnt>=1{
			gsel 4
			color 0,0,0
			if }EXPX!=0 & }EXPY!=0:	line }EXX,}EXY,}EXPX,}EXPY	//OՕ`
		}
		gsel 0
		}EXPX=}EXX
		}EXPY=}EXY
		//human count(zxcv{^񐔂ƎԂێ)
		Repeat 4	//ϐ4
			=0
			if cnt = 0 & gkZ = 1:=1
			if cnt = 1 & gkX = 1:=1
			if cnt = 2 & gkC = 1:=1
			if cnt = 3 & gkV = 1:=1
			if =1{
				l(cnt)+=1.0*XLbvt[/FPS
				lBin(cnt,݂Bin)+=1.0*XLbvt[/FPS
				lt[(cnt, Cnt)=1
				if lgK[(cnt)=0{
					l(cnt)++	//Õt[ŉĂȂꍇ̂݉񐔂
					lBin(cnt,݂Bin)++
					lgK[(cnt)=1
				}
			}
			if =0{
				lgK[(cnt)=0
			}
		Loop
		
		//͏I
		if Cnt>=̓t[-1{	//wt[ɒB͏I
			//dialog ""+݂Bin,0
			title "1"
			//Bin=݂Bin
			͒=0
			{(10)=-1
			OpX=pX
			cvcloseavi
			notesel 
			//1s
		
			+=","
			Repeat 5
				Repeat 쐻C{^
					+=","
				Loop
			Loop
			Repeat 8
				+=","
			Loop
			
			Repeat 5
				if cnt=0:P="RestingDuration Area"
				if cnt=1:P="MovedDistance(pixel) Area"
				if cnt=2:P="EntryNum Area"
				if cnt=3:P="ContactDuration Area"
				if cnt=4:P="ContactNum Area"
				Repeat 쐻C{^
					Num=cnt+1
					Repeat Bin
						if cnt=0{
							+=P+str(Num)+","
						}
						if cnt>=1{
							+=","
						}
					Loop
				Loop
			Loop
			Repeat 2
				ocnt=cnt
				Repeat 4 
					if cnt=0:+="Z "
					if cnt=1:+="X "
					if cnt=2:+="C "
					if cnt=3:+="V "
					if ocnt=0:+="Duration"
					if ocnt=1:+="Times"
					Repeat Bin
						+=","
					Loop
				Loop
			Loop
			if ŏ̃}EX = 1{
				noteadd 
			}
			
			//2-3s
			ږ1+="Name,"
			1+=""+O+","
			Repeat 쐻C{^
				Num=cnt+1
				ږ1+="TotalRestingDuration Area"+str(Num)+","
				ږ2+="TotalDistance(pixel) Area"+str(Num)+","
				ږ3+="TotalEntryNum Area"+str(Num)+","
				ږ4+="TotalContactDuration Area"+str(Num)+","
				ږ5+="TotalContactNum Area"+str(Num)+","
				
				1+=str(strf("%.2f",؍ݎ(Num)))+","
				2+=str(ړ(Num))+","
				3+=str(N(Num))+","
				4+=str(strf("%.2f",ڐG(Num)))+","
				5+=str(ڐG(Num))+","
			Loop
			Repeat 4
				if cnt=0{
					ږ6+="Z"
					ږ7+="Z"
				}
				if cnt=1{
					ږ6+="X"
					ږ7+="X"
				}
				if cnt=2{
					ږ6+="C"
					ږ7+="C"
				}
				if cnt=3{
					ږ6+="V"
					ږ7+="V"
				}
				ږ6+=" Duration,"
				ږ7+=" Times,"
				
				6+=str(strf("%.2f",l(cnt)))+","
				7+=str(l(cnt))+","
			Loop
			Repeat 5	//ʍږ
				Repeat 쐻C{^
					Repeat Bin
						ږ8+=str(cnt+1)+","
					Loop
				Loop
			Loop
			Repeat 2	//ʍږ
				Repeat 4
					Repeat Bin
						ږ9+=str(cnt+1)+","
					Loop
				Loop
			Loop
			if ŏ̃}EX = 1{
				noteadd ږ1+ږ2+ږ3+ږ4+ږ5+ږ6+ږ7+ږ8+ږ9	//2sډM
				//ŏ̃}EX = 0
			}
			//3s
			Repeat 5	//ʍږ
				oocnt=cnt
				Repeat 쐻C{^
					ocnt=cnt+1
					Repeat Bin
						if oocnt=0:8+=str(strf("%.2f",؍ݎBin(ocnt,cnt+1)))+","
						if oocnt=1:8+=str(ړBin(ocnt,cnt+1))+","
						if oocnt=2:8+=str(NBin(ocnt,cnt+1))+","
						if oocnt=3:8+=str(strf("%.2f",ڐGBin(ocnt,cnt+1)))+","
						if oocnt=4:8+=str(ڐGBin(ocnt,cnt+1))+","
					Loop
				Loop
			Loop
			title "2"
			Repeat 2	//lԁAl
				oocnt=cnt
				Repeat 4	//human count z,x,c,v key
					ocnt=cnt
					Repeat Bin
						if oocnt=0:9+=str(strf("%.2f",lBin(ocnt,cnt+1)))+","
						if oocnt=1:9+=str(lBin(ocnt,cnt+1))+","
					Loop
				Loop
			Loop
			noteadd 1+2+3+4+5+6+7+8+9	//3sډM

			title "3"
			gsel 4
			=strlen(pX)
			pXƖO=strmid(pX,0,-4)	//.aviȊORs[
			bmpsave pXƖO+".bmp"	//OՂۑ
			gsel 0
			title "4"


			//t[
			if ŏ̃}EX=1{
				t[+=","
				Repeat ̓t[
					t[+=str(cnt+1)+","
				Loop
				t[+="\n"
			}
			t[+=O+"\n"
			Repeat 쐻C{^
				ocnt=cnt
				t[+="Area "+(ocnt+1)+","
				Repeat ̓t[
					t[+=str(C؍݃t[(1+ocnt, cnt))+","
				Loop
				t[+="\n"
			Loop
			title "5"
			Repeat 4	//human countrawf[^̏o
				sdim ,
				ocnt=cnt
				if ocnt=0{
					t[+="Z key,"
				}
				if ocnt=1{
					t[+="X key,"
				}
				if ocnt=2{
					t[+="C key,"
				}
				if ocnt=3{
					t[+="V key,"
				}
				Repeat ̓t[
					t[+=str(lt[(ocnt, cnt))+","
				Loop
				t[+="\n"
			Loop
			title "6"

			//t[ړʂ̋L
			//DistancePerFrame
			if ŏ̃}EX=1{
				DistancePerFrame+="Name,"
				Repeat ̓t[
					DistancePerFrame+=str(cnt+1)+","
					title "6:"+cnt
				Loop
				title "6.1"
				DistancePerFrame+="\n"
			}
			title "7"
			DistancePerFrame+=O+","
			title "8"
			Repeat ̓t[
				DistancePerFrame+=str(t[ړ(cnt))+","	//ړʂL
				title "8:"+cnt
			Loop
			title "9"
			DistancePerFrame+="\n"
			title "10"

			
			//}EXW̋L
			//MouseCoordinate
			if ŏ̃}EX=1{
				MouseCoordinate+=","
				Repeat ̓t[
					MouseCoordinate+=str(cnt+1)+","
				Loop
				MouseCoordinate+="\n"
			}
			MouseCoordinate+=O
			MouseCoordinate+="\n"
			Repeat 2
				if cnt=0{
					MouseCoordinate+="X:,"
					Repeat ̓t[
						MouseCoordinate+=str(؍݈ʒuX(cnt))+","	//ړʂL
					Loop
				}
				if cnt=1{
					MouseCoordinate+="Y:,"
					Repeat ̓t[
						MouseCoordinate+=str(؍݈ʒuY(cnt))+","	//ړʂL
					Loop
				}
				MouseCoordinate+="\n"
			Loop

			title "11"
			if A̓[h=0{
				Message=1
				//dialog "Analysis is finished",0
			}
			if A̓[h=1{	//A̓[hł
				A͐++
				if A͐ < now_num{	//܂͓悪cĂ
					O=filename2(A͐)
					pX=SeqPath+"\\"+filename2(A͐)
					gsel 2
					cls
					cvcloseavi
					cvbuffer 0	//CVobt@(p1Fobt@[ԍAp2,3Fobt@[TCY)
					cvopenavi pX,0	//avit@Cobt@[(p2)ɊJiŕKvj
					cvgetavi			//̃R}cvobt@[ɃRs[
					gosub *MainWindowResize
					cvflip 0,0	//摜㉺t](Ȃ㉺܂ɂȂ)
					cvgetimg 0	//buffer2։摜]
					gsel 0
					gosub *StartAnalysis
				}
				if A͐ = now_num{	//SĂ̓͂IĂ
					//A̓[h=0
					A͐=0
					//redraw 1
					//redraw 0
					Message=2
					//dialog "Batch analysis is finished",0
					͒=0
					ob`͏I=1
				}
			}
			ŏ̃}EX=0
		}
	}
	//`**********************************************************************************************************************************
	gsel 0
	if ͒=0{
		gmode 0
		pos 捶X,捶Y
		
		if SplitCorrectMode=0{
			pos 捶X,捶Y
			gcopy 2,0,0,ʕ,ʍ	//Cʂ։摜] 
		}
		if SplitCorrectMode=1{
			pos 捶X,捶Y
			gcopy 2,FixX,0,FixWidth,ʍ	//Cʂ։摜] 
			pos 捶X+FixWidth,捶Y
			gcopy 2,0,0,FixX,ʍ	//Cʂ։摜] 
		}
		
		pget mx,my
		R=ginfo(16):G=ginfo(17):B=ginfo(18)
		|CgPx=R+G+B
	}
	
	//color 0,0,0:pos 5,555:	mes "View"	//
	color 0,0,0: pos 5,C\{^Y-25:	mes "View"
	//color 0,0,0:pos 5,625:	mes "Control"
	color 0,0,0: pos 5,CI{^Y-25:	mes "Control"

	gcopy 3,0,0,pX,pY
	gsel 98
	UiX = 5
	VtgY = 5
	//UiX=1180
	ԍ=0
	color 0,0,0
	pos UiX,VtgY:	mes "Video FPS":				X(ԍ)=UiX:	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Video length(sec)":		Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Color threshold(0-765)":	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Minimum pixel num"	:		Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Resol. down-sample(1-)":	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Frame down-sample(1-)":	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Bin duration(sec)"	:		Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "WaitTime(ms)/frame (0-)":	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Looseness length(0-)":		Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	pos UiX,VtgY:	mes "Pen size":					Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	Y(ԍ)=VtgY:	ԍ++:	VtgY+=55
	gsel 0
	
	\=0
	repeat 쐻C{^	//C`
		ocnt=cnt+1
		repeat {^ő吔
			if {(cnt)=2 & {C(cnt)=ocnt & {(cnt)=1{
				\=1
			}
		loop
		if \=1{
			pos 0,0
			a=cnt+5
			color 0,0,0
			gmode 4,,,50
			gcopy a,0,0,ʕ,ʍ
		}
		\=0
	loop
	if 샂[h=4 | 샂[h=7{
		if mx>0 & mx<ʕ & my>45 & my<C\{^Y-25{
			color 0,0,0	
			line mx+M/2,my-M/2,mx-M/2,my-M/2	//M̕`͈͕\
			line mx+M/2,my+M/2
			line mx-M/2,my+M/2
			line mx-M/2,my-M/2
		}
	}
	if 샂[h=2 & RoiPhase=1 | 샂[h=5 & RoiPhase=1{	//lp`
		RoiX1 = n_X
		RoiY1 = n_Y
		RoiX2 = I_X
		RoiY2 = I_Y
		color 0,0,0
		line RoiX2, RoiY1,RoiX1, RoiY1	//`
		line RoiX2, RoiY2
		line RoiX1, RoiY2
		line RoiX1, RoiY1
		pos mx,my-20
		mes "r="+(I_X-n_X)+","+(I_Y-n_Y)+" (pixel)"
	}
	if 샂[h=3 & RoiPhase=1 | 샂[h=6 & RoiPhase=1{	//~`
		RoiX1 = n_X-(I_X-n_X)
		RoiY1 = n_Y-(I_Y-n_Y)
		RoiX2 = I_X//-(n_X-I_X)/2
		RoiY2 = I_Y//-(n_Y-I_Y)/2
		color 0,0,0
		circle  RoiX2, RoiY2, RoiX1, RoiY1,0	//`
		pos mx,my-20
		mes "r="+(RoiX2-RoiX1)/2+","+(RoiY2-RoiY1)/2+" (pixel)"
	}
	
	repeat {^ő吔
		if {gp(cnt)=0:break
		if {(cnt)=-1:color 200,200,200
		if {(cnt)=1:color 0,0,0
		boxf {X(cnt),{Y(cnt),{X(cnt)+{(cnt),{Y(cnt)+{(cnt)
		pos {X(cnt)+5,{Y(cnt)+5
		if {(cnt)=-1:color 0,0,0
		if {(cnt)=1:color 255,255,255
		mes ""+{(cnt)
	loop
	//title "|C^W x:"+mx+" y:"+my+" IC"+IC+" 샂[h"+샂[h+" ͒"+͒+"RoiPhase"+RoiPhase+ICF+" d}EXpx\:"+d}EXpx\+" }EXx:"+}EXx+" d:"+d}EX
	//title "CurrentFrame:"+Cnt+"  Cursor X:"+mx+" Y:"+my+"  Mice coordinate X:"+int(}EXx)+" Y:"+int(}EXy)+"  Weight coordinate X:"+int(dX)+" Y:"+int(dY)
	//title ""+ږ1
	
	I=-1
	REPEAT 13
		if mxPara>=X(0) & myPara>=Y(cnt) & mxPara<=X(0)+ & myPara<=Y(cnt)+{
			I=cnt
			break
		}
	LOOP
	if I!=-1{
		color 240,240,240
		boxf 5,500,ʕ-5,550
		color 0,0,0
		pos 10,503
		mes ""+(I)
	}
	color 0,0,0
	if mx>0 & mx<ʕ & my>45 & my<C\{^Y-25{
		line mx-10,my,mx+10,my	//cross hair`
		line mx,my-10,mx,my+10
		if mx < ʕ-50{
			pos mx+10,my+10
		}
		if mx >= ʕ-50{
			pos mx-80,my+10
		}
		mes "("+mx+","+my+")"
	}

	redraw 1
	redraw 0
	
	
	gsel 99
	gmode 0
	pos 0,0:color 0,0,0:gcopy 3,0,0,ʕ,ʍ	//ʃNA
	
	color 0,0,0
	pos 5,5
	
	if ͒=1{
		mes "Current frame num: "+(Cnt+1)+" / "+̓t[
	}
	//debugp
	//mes "쐻C{^:	"+쐻C{^
	//mes "Total analyzed pixel num:	"+̓sNZ
	//mes "ʕ:			"+ʕ
	//mes "ʍ:		"+ʍ
	//mes "̓TCYX:		"+̓TCYX
	//mes "̓TCYY:		"+̓TCYY
	//mes "͖x:		"+͖x
	//mes "mxPara:		"+mxPara;
	//mes "myPara:		"+myPara
	mes "Movie size x:"+CVX+" y:"+CVY
	mes "Mice pixel num: "+
	
	Repeat 쐻C{^
		mes "Area"+(cnt+1)+"  Resting: "+strf("%.2f",؍ݎ(cnt+1))+" sec  MovedDistance: "+ړ(cnt+1)+"  EntryNum: "+N(cnt+1)+"  Contact: "+strf("%.2f",ڐG(cnt+1))+" sec  ContactNum: "+ڐG(cnt+1)
	Loop
	mes "Human count"
	Repeat 4
		if cnt=0: mes "  Zkey:"+strf("%.2f",l(cnt))+" sec  times:"+l(cnt)
		if cnt=1: mes "  Xkey:"+strf("%.2f",l(cnt))+" sec  times:"+l(cnt)
		if cnt=2: mes "  Ckey:"+strf("%.2f",l(cnt))+" sec  times:"+l(cnt)
		if cnt=3: mes "  Vkey:"+strf("%.2f",l(cnt))+" sec  times:"+l(cnt)
	Loop
	mes "Mouse cursor\n  x: "+mx+"  y: "+my
	mes "Color intensity(R+G+B): "+|CgPx
	
	mes "Data path:"
	mes pX
	title "Result window"
	redraw 1
	redraw 0
	if ͒=0:await 1
	if ͒=1:await ͑x
	gsel 0
	title "Mr. Behavior - Tracker"
	if Message=1{
		//dialog "Analysis is finished",0
		Message=0
		//OA̓[h=0
	}
	if Message=2{
		dialog "Batch analysis is finished",0
		Message=0
		//OA̓[h=1
	}
	Ot[}EXo=0
	if  < Œᓖ |  <= 0{
		Ot[}EXo=1
	}
LOOP
	
*Open
	dialog "avi", 16, "avi movie"
	if stat = 1{
		pX=refstr
		split pX, "\\", PIt@C
		O=PIt@C(stat-1)

		gsel 2
		cls
		cvcloseavi
		//cvbuffer 0,CVX,CVY	//CVobt@(p1Fobt@[ԍAp2,3Fobt@[TCY)
		cvbuffer 0
		cvopenavi refstr,0	//avit@Cobt@[(p2)ɊJiŕKvj
		cvgetavi			//̃R}cvobt@[ɃRs[
		gosub *MainWindowResize
		//gsel 0
		cvflip 0,0	//摜㉺t](Ȃ㉺܂ɂȂ)
		cvgetimg 0	//buffer2։摜]

		斢J=0
		ŏ̃t[=1
		
		gsel 0
		A̓[h=0
		ob`͏I=0
	}
	return

*MainWindowResize	//
	cvgetinfo CVX,0,CVOBJ_INFO_SIZEX	//ɍ킹ăTCYςCV buffer̃TCYێ
	cvgetinfo CVY,0,CVOBJ_INFO_SIZEY
	Oʕ=ʕ
	Oʍ=ʍ
	ʕ=CVX+10
	ʍ=CVY+220
	if ʕ<1200{
		ʕ=1200	//ŒႱ̃TCYɂ
	}
	if ʍ<700{
		//ʍ=700
	}
	C\{^Y = CVY+100
	CI{^Y = CVY+170
	Repeat {^ő吔
		if {gp(cnt)=1{
			if {(cnt)=2{
				{Y(cnt)=C\{^Y
			}
			if {(cnt)=3{
				{Y(cnt)=CI{^Y
			}
		}
	Loop
	gsel 0
	ʍX=ginfo(4): ʍY=ginfo(5)
	screen 0,ʕ,ʍ,0,ʍX,ʍY
	screen 3,ʕ,ʍ
	dim ͂邩,ʕ,ʍ
	buffer 97,ʕ,ʍ
	Repeat 12
		if cnt=obt@-5{
			break
		}
		gsel 97
		color 0,0,0
		boxf 0,0,ʕ,ʍ
		pos 0,0
		gcopy cnt+5,0,0,Oʕ,Oʍ	//ROIꎞ
		//gcopy cnt+5,0,0,1000,1000
		buffer cnt+5,ʕ,ʍ
		boxf 0,0,ʕ,ʍ
		gsel cnt+5
		pos 0,0
		gcopy 97,0,0,Oʕ,Oʍ	//ROI񕜌
	Loop
	gsel 2
	buffer 2,CVX,CVY
	return
	
*AddChannel
	if 쐻C{^ < 11{
		repeat {^ő吔
			if {gp(cnt)=0{
				쐻C{^++
				if 쐻C{^=1{R=0:R=255:B=0}
				if 쐻C{^=2{R=0:R=0:B=255}
				if 쐻C{^=3{R=149:R=0:B=181}
				if 쐻C{^=4{R=255:R=242:B=0}
				if 쐻C{^=5{R=255:R=242:B=0}
				if 쐻C{^=6{R=255:R=108:B=0}
				if 쐻C{^=7{R=11:R=255:B=249}
				if 쐻C{^=8{R=0:R=3:B=132}
				if 쐻C{^=9{R=255:R=36:B=255}
				if 쐻C{^=10{R=140:R=151:B=149}
				if 쐻C{^=11{R=45:R=47:B=47}
				{gp(cnt)=1:	{(cnt)=2:	{X(cnt)=C{^X:		{Y(cnt)=C\{^Y:{(cnt)=100:		{(cnt)=30:		{(cnt)=1:		{(cnt)="Area"+쐻C{^:	{C(cnt)=쐻C{^	:{F(cnt,0)=R	:{F(cnt,1)=G	:{F(cnt,2)=B
				if ŏ=1:{(cnt)=1
				{gp(cnt+1)=1:{(cnt+1)=3:{X(cnt+1)=C{^X:	{Y(cnt+1)=CI{^Y:{(cnt+1)=100:	{(cnt+1)=30:	{(cnt+1)=-1:	{(cnt+1)="Area"+쐻C{^:	{C(cnt+1)=쐻C{^:	
				{F(cnt+1,0)=R:{F(cnt+1,1)=G:{F(cnt+1,2)=B
				if ŏ=1:{(cnt+1)=1
				C{^X+102
				buffer obt@,ʕ,ʍ
				color 0,0,0:boxf 0,0,ʕ,ʍ
				gsel 0
				obt@++

				repeat {^ő吔
					if {gp(cnt)=1 & {(cnt)=3{
						{(cnt)=-1
					}
				loop
				{(cnt+1)=1
				IC={C(cnt)

				break
			}
		loop
	}
	
	return

*GetSelectedLayerColor
	if IC=1:color 255,0,0
	if IC=2:color 0,255,0
	if IC=3:color 0,0,255
	if IC=4:color 149,0,181
	if IC=5:color 255,242,0
	if IC=6:color 255,108,0
	if IC=7:color 11,255,249
	if IC=8:color 0,3,132
	if IC=9:color 255,36,255
	if IC=10:color 140,151,149
	if IC=11:color 45,47,47
	ICF=ginfo(16)
	ICF=ginfo(17)
	ICF=ginfo(18)
	return

*SaveParameters
	chdir ProgramPath
	bsave "VideoFPS.at",FPS
	bsave "VideoLength.at",撷
	bsave "ColorThreshold.at",臒l
	bsave "MinimumPixelNum.at",Œᓖ
	bsave "DownSample.at",͖x
	bsave "FrameSkip.at",XLbvt[
	bsave "BinDur.at",Bin
	bsave "WaitTimePerFrame.at",͑x
	bsave "Looseness.at",͏dR
	bsave "PenSize.at",M
	bsave "IsWhiteAnimal.at",IsWhiteAnimal
	bsave "LeaveCursorWhileLost.at",LeaveCursorWhileLost
	bsave "BackNoiseFilterOn.at",BackNoiseFilterOn
	return

*StartAnalysis
	͊Jn~=0
	Cnt=-1	
	//̓t[=͉̓t[/XLbvt[
	̓t[=FPS*撷/XLbvt[
	Bin=(̓t[*XLbvt[)/(Bin*FPS)+1
	if ob`͏I=1{	//ob`2ڂ̉͂̏ꍇ̏
		O=filename2(0)
		pX=SeqPath+"\\"+filename2(0)
	}

	gsel 2
	cls
	cvcloseavi
	cvbuffer 0	//CVobt@(p1Fobt@[ԍAp2,3Fobt@[TCY)
	cvopenavi pX,0	//avit@Cobt@[(p2)ɊJiŕKvj
		
	
	Repeat ʍ
		ocnt=cnt
		Repeat ʕ
			͂邩(cnt,ocnt)=0	//
		Loop
	Loop
	̓TCYX=0
	̓TCYY=0
	so=0
	Repeat 쐻C{^	//F̂Wc鏈
		a=5+cnt
		gsel a
		Repeat ʍ/͖x
			ocnt=cnt
			Repeat ʕ/͖x
				pget cnt*͖x,ocnt*͖x
				r=ginfo(16):g=ginfo(17):b=ginfo(18)
				if r!=0|g!=0|b!=0{	//FtĂꍇ
					͂邩(cnt*͖x,ocnt*͖x)=1	//̍W̓sNZw(̏ɂFtĂȂsNZɂ蔻͕tȂ)
					if ̓TCYX < cnt*͖x:	̓TCYX=cnt*͖x	//łEɂFsNZ͔͈͂̍ɂ
					if ̓TCYY < ocnt*͖x:	̓TCYY=ocnt*͖x
				}
			Loop
		Loop
	Loop
	if ̓TCYX=0 & ̓TCYY=0{
		dialog "There is no area!",1,""
		͊Jn~=1
	}
	dim C؍݃t[,12,̓t[
	dim lt[,4,̓t[
	
	if ͊Jn~=0{
		͒=1
		{(cnt)*=-1
		buffer 4,̓TCYX,̓TCYY	//OՉ摜pobt@
		color 255,255,255
		boxf 0,0,̓TCYX,̓TCYY
		gsel 0
		Repeat 쐻C{^
			؍ݎ(1+cnt)=0.0
			ړ(1+cnt)=0
			N(1+cnt)=0
			ڐG(1+cnt)=0.0
			ڐG(1+cnt)=0
			ڐGԃgK[(1+cnt)=0
			ڐG񐔃gK[(1+cnt)=0
		Loop
		Repeat 4
			l(cnt) = 0.0
			l(cnt) = 0
		Loop
		ږ1="":ږ2="":ږ3="":ږ4="":ږ5="":ږ6="":ږ7="":ږ8="":ږ9=""
		1="":2="":3="":4="":5="":6="":7="":8="":9=""
		=""
		
		
		ddim ؍ݎBin,12,Bin+1
		dim ړBin,12,Bin+1
		dim NBin,12,Bin+1
		dim ڐGBin,12,Bin+1
		ddim ڐGBin,12,Bin+1
		dim Ot[,12
		ddim t[ړ,̓t[
		dim ؍݈ʒuX,̓t[
		dim ؍݈ʒuY,̓t[
		ddim lBin,4,Bin+1
		dim lBin,4,Bin+1
		
		Message=0
		}EXXg=0
		gosub *SaveParameters
	}
	
	gsel 98
	objenable 0,0	//rŒlςȂ悤͗s
	objenable 1,0
	objenable 4,0
	objenable 5,0
	objenable 6,0
	gsel 2
	return

*SaveRoi
	CurrBuffer=0
	Repeat ̓TCYY
		Y=cnt
		Repeat ̓TCYX
			X=cnt
			dim LayerExist,11
			Repeat 쐻C{^
				CurrBuffer=cnt+5
				gsel CurrBuffer
				pget X, Y
				if ginfo(16)!=0 | ginfo(17)!=0 | ginfo(17)!=0{	//if ROI exist in this pixel
					LayerExist(cnt)=1
				}
			Loop
			l = LayerExist(0)+LayerExist(1)*2+LayerExist(2)*4+LayerExist(3)*8+LayerExist(4)*16+LayerExist(5)*32+LayerExist(6)*64+LayerExist(7)*128+LayerExist(8)*256+LayerExist(9)*512+LayerExist(10)*1024
			RoiSaveArray(X, Y)=l
		Loop
	Loop

	RoiSaveArray(0, ʍ)=̓TCYX
	RoiSaveArray(1, ʍ)=̓TCYY
	RoiSaveArray(2,ʍ) = 쐻C{^
	
	dialog "",17,
	if stat=1{	
		SaveName=refstr
		SaveName2=""
		split SaveName, ".", SaveName2
		bsave SaveName2+".roi",RoiSaveArray
	}
	gsel 0
	return
	
*LoadRoi
	dialog "roi",16,"*.roi"
	if stat=1{
		//gsel IC+4
		bload refstr, RoiSaveArray	//Load

		̓TCYX=RoiSaveArray(0, ʍ)
		̓TCYY=RoiSaveArray(1, ʍ)
		//dialog RoiSaveArray(2, ʍ),0
		Repeat 11
			if 쐻C{^ < RoiSaveArray(2, ʍ){
				gosub *AddChannel
			}
		Loop
		
		Repeat ̓TCYY
			y=cnt
			Repeat ̓TCYX
				x=cnt
				l=RoiSaveArray(x, y)
				Repeat 11
					LayerExist(cnt)=l\2
					l/=2
				Loop
				
				Repeat 쐻C{^
					IC=cnt+1
					CurrBuffer=cnt+5
					gsel CurrBuffer
					if LayerExist(cnt)=1{
						gosub *GetSelectedLayerColor	//݂̑IGA̐F擾
						color ICF,ICF,ICF	//IC̐FɃGA̐FύX
						pset x,y
						//dialog ""+x+" "+y,0
					}
				Loop
			Loop
		Loop
		gsel 0
	}
	return
	
*exit
	gosub *SaveParameters
	end